import * as types from './mutations_type'

const actions = {
  updateCollapse({ commit }) {
    commit(types.SET_IS_COLLAPSE)
  },
  updateUserInfo({ commit }, userInfo) {
    commit(types.SET_USER_INFO, userInfo)

    commit(types.SET_USER_PERMISSIONS, getPermissionValues(userInfo))
  }
}

function getPermissionValues(userInfo) {
  let permissionValues = []

  userInfo.permissions.forEach(item =>
    permissionValues.push(item.permissionValue)
  )

  userInfo.roles.forEach(item => {
    item.permissions.forEach(permission =>
      permissionValues.push(permission.permissionValue)
    )
  })

  return Array.from(new Set(permissionValues))
}

export default actions
